#ifndef QHTTPCONTENTTYPE_H
#define QHTTPCONTENTTYPE_H

#include <QByteArray>
#include "nlohmann/json.hpp"

namespace QtNet {

// application/json
template <typename T>
struct QHttpContentType
{
    static void fromByteArray(T &jsonObject, const QByteArray &bjson) {
        try {
            nlohmann::json json = nlohmann::json::parse(bjson.data());
            jsonObject = json.get<T>();
        } catch (const nlohmann::json::exception& e) {
            throw;
        }
    }

    static void toByteArray(const T &jsonObject, QByteArray &bjson) {
        try {
            nlohmann::json j = jsonObject;
            std::string json_str = j.dump();
            bjson = bjson.fromStdString(json_str);
        } catch (const nlohmann::json::exception& e) {
            throw;
        }
    }
};
}

#define QHTTPCONTENTTYPE_BEGIN_DECLARE_CLASS(type) \
    namespace QtNet { \
    template <> \
    struct QHttpContentType<type> \
{
#define QHTTPCONTENTTYPE_END_DECLARE_CLASS \
    }; \
    }

#endif // QHTTPCONTENTTYPE_H
